SwitchCompat

(SESL variant) SwitchCompat is a complete backport of the core Switch widget that brings the visuals and functionality of the toggle widget to older versions of the Android platform. Unlike other widgets in this package, SwitchCompat is not automatically used in layouts that include the <Switch> element. Instead, you need to explicitly use <androidx.appcompat.widget.SwitchCompat> and the matching attributes in your layouts.

The thumb can be tinted with setThumbTintList and setThumbTintMode APIs as well as with the matching XML attributes. The track can be tinted with setTrackTintList and setTrackTintMode APIs as well as with the matching XML attributes.

Supported attributes include:

For more information, see the Toggle Buttons guide.

Inheritors

Constructors

Link copied to clipboard
constructor(@NonNull context: @NonNull Context)
Construct a new Switch with default styling.
constructor(@NonNull context: @NonNull Context, @Nullable attrs: @Nullable AttributeSet)
Construct a new Switch with default styling, overriding specific style attributes as requested.
constructor(@NonNull context: @NonNull Context, @Nullable attrs: @Nullable AttributeSet, defStyleAttr: Int)
Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.

Properties

Link copied to clipboard
open var mTrackMargin: Int

Functions

Link copied to clipboard
open fun canHapticFeedback(isChecked: Boolean): Boolean
Link copied to clipboard
open fun draw(@NonNull c: @NonNull Canvas)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun getShowText(): Boolean
Indicates whether the on/off text should be displayed.
Link copied to clipboard
Returns whether the track should be split by the thumb.
Link copied to clipboard
Get the minimum width of the switch in pixels.
Link copied to clipboard
open fun getSwitchPadding(): Int
Get the amount of horizontal padding between the switch and the associated text.
Link copied to clipboard
Returns the text displayed when the button is not in the checked state.
Link copied to clipboard
Returns the text displayed when the button is in the checked state.
Link copied to clipboard
Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Link copied to clipboard
Get the horizontal padding around the text drawn on the switch itself.
Link copied to clipboard
@Nullable
open fun getThumbTintList(): @Nullable ColorStateList
Link copied to clipboard
open fun getThumbTintMode(): @Nullable PorterDuff.Mode
Link copied to clipboard
Get the drawable used for the track that the switch slides within.
Link copied to clipboard
@Nullable
open fun getTrackTintList(): @Nullable ColorStateList
Link copied to clipboard
open fun getTrackTintMode(): @Nullable PorterDuff.Mode
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun setAllCaps(allCaps: Boolean)
Link copied to clipboard
open fun setChecked(checked: Boolean)
Link copied to clipboard
Link copied to clipboard
open fun setEmojiCompatEnabled(enabled: Boolean)
Configure emoji fallback behavior using EmojiCompat.
Link copied to clipboard
open fun setFilters(filters: @NonNull Array<InputFilter>)
Link copied to clipboard
open fun setShowText(showText: Boolean)
Sets whether the on/off text should be displayed.
Link copied to clipboard
open fun setSplitTrack(splitTrack: Boolean)
Specifies whether the track should be split by the thumb.
Link copied to clipboard
open fun setSwitchMinWidth(pixels: Int)
Set the minimum width of the switch in pixels.
Link copied to clipboard
open fun setSwitchPadding(pixels: Int)
Set the amount of horizontal padding between the switch and the associated text.
Link copied to clipboard
open fun setSwitchTextAppearance(context: Context, resid: Int)
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Link copied to clipboard
open fun setSwitchTypeface(typeface: Typeface)
Sets the typeface in which the text should be displayed on the switch.
open fun setSwitchTypeface(tf: Typeface, style: Int)
Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.
Link copied to clipboard
open fun setTextOff(textOff: CharSequence)
Sets the text displayed when the button is not in the checked state.
Link copied to clipboard
open fun setTextOn(textOn: CharSequence)
Sets the text displayed when the button is in the checked state.
Link copied to clipboard
open fun setThumbDrawable(thumb: Drawable)
Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Link copied to clipboard
open fun setThumbResource(resId: Int)
Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Link copied to clipboard
open fun setThumbTextPadding(pixels: Int)
Set the horizontal padding around the text drawn on the switch itself.
Link copied to clipboard
open fun setThumbTintList(@Nullable tint: @Nullable ColorStateList)
Applies a tint to the thumb drawable.
Link copied to clipboard
open fun setThumbTintMode(tintMode: @Nullable PorterDuff.Mode)
Specifies the blending mode used to apply the tint specified by setThumbTintList} to the thumb drawable.
Link copied to clipboard
open fun setTrackDrawable(track: Drawable)
Set the drawable used for the track that the switch slides within.
Link copied to clipboard
open fun setTrackResource(resId: Int)
Set the drawable used for the track that the switch slides within.
Link copied to clipboard
open fun setTrackTintList(@Nullable tint: @Nullable ColorStateList)
Applies a tint to the track drawable.
Link copied to clipboard
open fun setTrackTintMode(tintMode: @Nullable PorterDuff.Mode)
Specifies the blending mode used to apply the tint specified by setTrackTintList to the track drawable.
Link copied to clipboard
open fun toggle()